package org.gary.news.service.impl;

import static lengreen.util.StringUtils.isNull;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.gary.base.model.Image;
import org.gary.base.service.ImageService;
import org.gary.core.factories.impl.BasicServiceFactoryImpl;
import org.gary.core.util.ResultMessage;
import org.gary.news.dao.NewsDao;
import org.gary.news.model.News;
import org.gary.news.service.NewsService;
import org.springframework.stereotype.Component;
/**
 * 新闻service实现类
 * @author Gary
 *
 */
@Component("newsService")
public class NewsServiceImpl extends BasicServiceFactoryImpl<News> implements
		NewsService {
	private ImageService imageService;
	private NewsDao newsDao;
	
	/**
	 * 重写basicServicefactoryimpl中的Save方法， 重写以加入日志模块，并记录日志
	 */
	@Override
	public String save(News entity) {
		if (isNull(entity.getTitle())) {
			return ResultMessage.news.ADD_NONE;
		} else {
			for (Image image : entity.getImages()) {
				imageService.save(image);
			}
			// 在新闻保存之前记录一条日志
			getLoggerService().makeLog(entity.getClass().getName(),
					"开始保存一个新闻，" + entity);
			getBasicDaoFactory().save(entity);
			// 在新闻保存之后记录一条日志
			getLoggerService().makeLog(entity.getClass().getName(),
					"保存一个新闻成功，" + entity);
			return ResultMessage.news.ADD_SUCCESS;
		}
	}
	
	@Override
	public List<Image> loadImages(int productId) {
		return newsDao.loadImages(productId);
	}
	
	@Override
	public List<News> getNews(Map<String, Object> where,int page,int maxCount,Object[] orderBy) {
		return list(News.class, page, maxCount, where, orderBy);
	}

	@Override
	public List<News> getNews(int page, int maxCount, Object[] orderBy) {
		return list(News.class, page, maxCount, orderBy);
	}

	@Resource
	public void setImageService(ImageService imageService) {
		this.imageService = imageService;
	}

	public ImageService getImageService() {
		return imageService;
	}

	@Resource
	public void setNewsDao(NewsDao newsDao) {
		this.newsDao = newsDao;
	}

	public NewsDao getNewsDao() {
		return newsDao;
	}

	@Override
	public void addBrowse(int id) {
		newsDao.addBrowse(id);
	}

}
